package com.pactera.miyuangroup;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成
 *
 * @author Wangwu
 * @create 2018-02-08 上午11:31
 **/
public class Generate {
    public static void main(String[] args) {
        String packageName = "com.pactera.miyuangroup";
        String[] tableName = {"t_vehicle_info"};//{"sys_login_user","r_login_role","r_role_menu","r_vehicle_user","sys_login_user","sys_menu","sys_role","t_equipment_audit","t_goods_classification","t_goods_info","t_organization_structure","t_replenishment","t_replenishment_detailed","t_user_info","t_vehicle_info","t_vendor_info","t_vendor_passageway"};
        generateByTables("/Users/wangwu/Documents/CompanyProject/pactera/rice_source_replenishment_app/miyuangroup", packageName, tableName);

    }


    private static void generateByTables(String pojectpath, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        String dbUrl = "jdbc:mysql://114.55.172.102:3306/miyuangroup?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("mysql")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setTablePrefix(new String[]{"sys_","r_","t_"})
                .setNaming(NamingStrategy.underline_to_camel)
                //.setSuperEntityColumns(new String[]{"id", "del", "add_time", "up_time"})
                //.setSuperEntityClass("com.wangwu.service.shopping.common.BaseEntity")
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config
                .setActiveRecord(true)
                .setAuthor("WangWu")
                .setOutputDir(pojectpath + "/src/main/java")
                .setFileOverride(true)
                .setOpen(false)
                .setEnableCache(false)// XML 二级缓存
                .setBaseResultMap(true)// XML ResultMap
                .setBaseColumnList(false)// XML columList
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImp");
        //.setControllerName("%s-Controller");
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return pojectpath + "/src/main/resources/mapper/_" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);//不生成Xml
        tc.setController(null);//不生成Controller
        //tc.setService(null);//不生成Service
        //tc.setServiceImpl(null);//ServiceImpl
        //tc.setMapper(null);//Mapper
        //tc.setEntity(null);
        new AutoGenerator()
                .setTemplate(tc)
                .setGlobalConfig(config)
                .setCfg(cfg)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setServiceImpl("service.impl")
                                .setService("service")
                                .setMapper("db.mapper")
                                .setController("controller")
                                .setEntity("db.entity")
                ).execute();
    }
}
